package com.useeinfo.oa.modules.base.dao;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.CustomerRepair;
import com.useeinfo.oa.modules.base.entity.DataCat;
import com.useeinfo.oa.modules.repair.entity.RepairOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

@Repository("customerRepairDao")
public class CustomerRepairDao implements CrudDao<CustomerRepair> {

    private final static Logger logger = LoggerFactory.getLogger(CustomerRepair.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String deleteStatus = queryHash.get("deleteStatus");
        if (StringUtils.isNotBlank(deleteStatus)) {
            conditionHash.put("dc.deleteStatus = ?{paramIndex} ", StringConverters.null2Boolean(deleteStatus));
        }

        String magicLike = queryHash.get("magicLike");
        if (org.apache.commons.lang.StringUtils.isNotBlank(magicLike)) {
            conditionHash.put("dc.src like ?{paramIndex} ", "%" + magicLike + "%");
        }
        String magicId = queryHash.get("magicId");
        if (org.apache.commons.lang.StringUtils.isNotBlank(magicId)) {
            conditionHash.put("dc.id != ?{paramIndex} ", StringConverters.ToLong(magicId));
        }

        String dst = queryHash.get("src");
        if(StringUtils.isNotBlank(dst)){
            conditionHash.put("dc.src = ?{paramIndex}",StringConverters.null2String(dst));
        }

        String recordPath = queryHash.get("recordPath");
        if(StringUtils.isNotBlank(recordPath)){
            conditionHash.put("dc.recordPath = ?{paramIndex}) ",recordPath);
        }

        String callDate = queryHash.get("callDate");
        if (StringUtils.isNotBlank(callDate)) {
            conditionHash.put("(date_format(dc.callDate,'%Y-%m-%d %H:%i:%s') = ?{paramIndex}) ", callDate);
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(dc) from CustomerRepair dc ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<CustomerRepair> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select dc from CustomerRepair dc ", "order by dc.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<CustomerRepair> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, CustomerRepair.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public CustomerRepair findModel(Long id) {
        return entityManager.find(CustomerRepair.class, id);
    }

    @Override
    public Integer add(CustomerRepair model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("CustomerRepairDaoImpl添加customerRepair成功！");
        return 1;
    }

    @Override
    public Integer update(CustomerRepair model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        CustomerRepair customerRepair = entityManager.find(CustomerRepair.class, id);
        entityManager.remove(customerRepair);
        return 1;
    }

    @Override
    public Integer deleteLogic(CustomerRepair model) {
        model.setDeleteStatus(true);
        return 1;
    }

    public List<SelectVO> getCustomerRepairs(String name,String q){
        List<SelectVO> selectVOS = new ArrayList<>();
        String jpql = "SELECT distinct(s.src) FROM CustomerRepair s  WHERE 1 = 1 and s.deleteStatus = false ";
        if(StringUtils.isNotBlank(q)){
            jpql += "and s.src like '%" + q + "%' " ;
        }
        if(StringUtils.isNotBlank(name)){
            jpql += "and s.src = '" + name + "' " ;
        }
        TypedQuery<String> resultQuery = this.entityManager.createQuery(jpql, String.class);
        List<String> resultList = resultQuery.getResultList();
        for(String src : resultList){
            selectVOS.add(new SelectVO(src,src));
        }
        return selectVOS;
    }
}
